package 牛牛冲钻石;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        while (T-- > 0) {
            int n = in.nextInt();
            int k = in.nextInt();
            int w = 0, res = 0;
            String s = in.next();
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) == 'W' && ++w >= 3) {
                    res += k;
                } else if (s.charAt(i) == 'W') {
                    res++;
                } else {
                    w = 0;
                    res--;
                }
            }
            System.out.println(res);
        }
    }
}
